<?php
/**
 * admin service file Created by PhpStorm.
 * User: yang
 * Date: 2020/06/15
 */

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Service\Web\ForumService;

class ForumController extends Controller
{
    /**
     * 获取列表数据
     * @param int $type
     * @param int $tag
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function list($type = 0, $tag = 0, Request $request)
    {
        $get_data = $request->all();
        $typeAry = \App\Http\Service\Admin\ForumTypeService::getAll();

        if ($type) {
            $get_data['where']['type_id'] = $type;
        }
        if ($tag) {
            $get_data['where']['tag'] = $tag;
        }
        $get_data['where']['status'] = 0;

        $list = ForumService::getList($get_data);

        $host = ForumService::hostList();//最近热议

        $result = [
            'type' => $typeAry,
            'list' => $list,
            'host' => $host,
            'path' => '/forumList/' . $type,
            'type_id' => (int)$type,
            'tag' => (int)$tag,
            'allPath' => $request->path(),
            'sort' => $get_data['sort']
        ];

        return view('Web.jie.index', $result);
    }

    /**
     * 添加视图
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $result = \App\Http\Service\Admin\ForumTypeService::getAll();

        return view('Web.jie.create', ['type' => $result]);
    }

    /**
     * 添加操作
     * @param Request $request
     * @return array
     */
    public function insert(Request $request)
    {
        $post_data = $request->except(['_token']);
        $post_data['user_id'] = web_user_info()['id'];

        return ForumService::insert($post_data);
    }

    /**
     * 详情视图
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View\
     */
    public function detail($id = 0)
    {
        $result = ForumService::detail((int)$id);
        $result['type'] = \App\Http\Service\Admin\ForumTypeService::getAll();
        $result['host'] = ForumService::hostList();//最近热议

        return view('Web.jie.detail', $result);
    }

    /**
     * 编辑页面
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id = 0)
    {
        $result = ForumService::edit((int)$id);
        $result['type'] = \App\Http\Service\Admin\ForumTypeService::getAll();

        return view('Web.jie.edit', $result);
    }

    /**
     * 编辑操作
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $post_data = $request->except(['_token']);

        return ForumService::update($post_data);
    }

    /**
     * 删除操作
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        $id = $request->get('id', 0);

        return ForumService::delete((int)$id);
    }

    /**
     * 评论回复
     * @param Request $request
     * @return mixed
     */
    public function reply(Request $request)
    {
        $post_data = $request->except(['_token']);

        return ForumService::reply($post_data);
    }

    /**
     * 获取评论列表
     * @param Request $request
     * @return mixed
     */
    public function commentList(Request $request)
    {
        $get_data = $request->all();

        return ForumService::commentList($get_data);
    }

    /**
     * 个人操作
     * @param Request $request
     * @return mixed
     */
    public function operate(Request $request)
    {
        $post_data = $request->except(['_token']);

        $result = ForumService::operate($post_data);

        return json_encode($result);
    }
}